<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从数组中取出一段</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-shift.html">array_shift</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-splice.html">array_splice</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.array-slice" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_slice</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">array_slice</span> &mdash; <span class="dc-title">从数组中取出一段</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-slice-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>array_slice</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserve_keys</code><span class="initializer"> = false</span></span>
  ]] )</div>

    <p class="para rdfs-comment">
     <span class="function"><strong>array_slice()</strong></span> 返回根据 <em><code class="parameter">offset</code></em> 和
     <em><code class="parameter">length</code></em> 参数所指定的 <em><code class="parameter">array</code></em>
     数组中的一段序列。
    </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-slice-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">array</code></em></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">offset</code></em></dt>

     <dd>

      <p class="para">
        如果 <em><code class="parameter">offset</code></em> 非负，则序列将从 <em><code class="parameter">array</code></em>
     中的此偏移量开始。如果 <em><code class="parameter">offset</code></em> 为负，则序列将从
     <em><code class="parameter">array</code></em> 中距离末端这么远的地方开始。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">length</code></em></dt>

     <dd>

      <p class="para">
       如果给出了 <em><code class="parameter">length</code></em> 并且为正，则序列中将具有这么多的单元。如果给出了
     <em><code class="parameter">length</code></em> 并且为负，则序列将终止在距离数组末端这么远的地方。如果省略，则序列将从
     <em><code class="parameter">offset</code></em> 开始一直到 <em><code class="parameter">array</code></em> 的末端。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">preserve_keys</code></em></dt>

     <dd>

      <p class="para">
        注意 <span class="function"><strong>array_slice()</strong></span> 默认会重新排序并重置数组的数字索引。你可以通过将
     <em><code class="parameter">preserve_keys</code></em> 设为 <strong><code>TRUE</code></strong> 来改变此行为。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-slice-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回其中一段。
  </p>
 </div>

  <div class="refsect1 changelog" id="refsect1-function.array-slice-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.0.2</td>
       <td>
        增加了可选参数 <em><code class="parameter">preserve_keys</code></em> 。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-slice-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4884">
    <p><strong>Example #1 <span class="function"><strong>array_slice()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"d"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"e"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;returns&nbsp;"c",&nbsp;"d",&nbsp;and&nbsp;"e"<br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;returns&nbsp;"d"<br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;returns&nbsp;"a",&nbsp;"b",&nbsp;and&nbsp;"c"<br /><br />//&nbsp;note&nbsp;the&nbsp;differences&nbsp;in&nbsp;the&nbsp;array&nbsp;keys<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; c
    [1] =&gt; d
)
Array
(
    [2] =&gt; c
    [3] =&gt; d
)
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-slice-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-splice.html" class="function" rel="rdfs-seeAlso">array_splice()</a> - 把数组中的一部分去掉并用其它值取代</span></li>
    <li class="member"><span class="function"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a> - 释放给定的变量</span></li>
    <li class="member"><span class="function"><a href="function.array-chunk.html" class="function" rel="rdfs-seeAlso">array_chunk()</a> - 将一个数组分割成多个</span></li>
   </ul>
  </p>
 </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-shift.html">array_shift</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-splice.html">array_splice</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
